define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'learn/orgpay/index' + location.search,
                    add_url: 'learn/orgpay/add',
                    edit_url: 'learn/orgpay/edit',
                    del_url: 'learn/orgpay/del',
                    multi_url: 'learn/orgpay/multi',
                    table: 'org_cate',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('序号'),operate:false},
                        {field: 'name', title: __('单位名称'),operate:'like'},
                        // {field: 'parent_id', title: __('Parent_id')},
                        // {field: 'level', title: __('Level')},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'introduce', title: __('Introduce')},
                        // {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        // {field: 'mobile', title: __('Mobile')},
                        // {field: 'linkman', title: __('Linkman')},
                        // {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('课程到期时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, datetimeFormat:'YYYY-MM-DD'},
                        // {field: 'type', title: __('Type')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                commonSearch: false,
                search:true,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);

                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Table.api.init({
                extend: {
                    add_url: 'learn/orgpay/add'+ location.search,
                    table: 'learn_orgpay_add'
                }
            });
            var table2 = $('#table2');

            table2.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.add_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('序号'),operate:false},
                        // {field: 'number', title: __('课程编码'), operate:'LIKE'},
                        {field: 'name', title: __('单位名称'), operate:'LIKE'},
                        // {field: 'teacher.name', title: __('主讲老师'), operate:'LIKE'},
                        // {field: 'origin_price', title: __('Origin_price'), operate:'BETWEEN'},
                        // {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        // {field: 'visitor_origin_price', title: __('Visitor_origin_price') ,operate:false},
                        // {field: 'visitor_price', title: __('Visitor_price'),operate:false},
                        //{field: 'buy_numbers', title: __('Buy_numbers'), operate:'>='},
                        // {field: 'image', title: __('Image'),operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'admin.username', title: __('创建人'), operate:'LIKE'},
                        //{field: 'thumbs', title: __('Thumbs'),operate:false},
                        //{field: 'is_buy', title: __('Is_buy'),operate:false, searchList: {"0":__('Is_buy 0'),"1":__('Is_buy 1')}, formatter: Table.api.formatter.status},
                        // {field: 'status', title: __('Status'),operate:false, searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                commonSearch: false,
                search:true
            });
            // 为表格绑定事件
            Table.api.bindevent(table2);
            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                var ids = Table.api.selectedids(table2);  //所有选中的项
                var page = table2.bootstrapTable('getData');  //当前页面
                var url = 'learn/orgpay/addorg';
                Layer.confirm("<form id='check-piliang' class='form-horizontal' style='width: 600px;height: 300px;'  action='" + Fast.api.fixurl(url) + "' method='post'>" +
                    "<div class='form-group'> <label class='control-label col-xs-12 col-sm-2'><span style='color:red;'>*</span>开始时间:</label> <div class='col-xs-12 col-sm-8'> <input id='c-start_time' data-rule='required' class='form-control datetimepicker' data-date-format='YYYY-MM-DD' data-use-current='true' name='row[start_time]' type='text' value=''> </div> </div>"+
                    "<div class='form-group'>\n" +
                    "        <label class='control-label col-xs-12 col-sm-2'><span style='color:red;'>*</span>结束时间:</label>\n" +
                    "        <div class='col-xs-12 col-sm-8' style='position: relative;'>\n" +
                    "            <input id='c-end_time' class='form-control datetimepicker' data-date-format='YYYY-MM-DD' data-use-current='true' name='end_time' type='text' value=''>\n" +
                    "        </div>\n" +
                    "    </div>"+
                    "<input type='hidden' name='ids' value='' />" +
                    "</form>",
                    {
                        title: '批量添加付费单位',
                        btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)"],
                        yes: function (index, layero) {
                            submitExportForm(ids.join(","), layero, index);
                            return false;
                        },
                        btn2: function (index, layero) {
                            var ids = [];
                            $.each(page, function (i, j) {
                                ids.push(j.id);
                            });
                            submitExportForm(ids.join(","), layero, index);

                            return false;
                        },
                    });
                $('#check-piliang').parents('.layui-layer-content').parents('.layui-layer-dialog').css({'width':'600px','left':'440px','top':'100px'});
                var form = $("#check-piliang");
                if ($(".datetimepicker", form).size() > 0) {
                    require(['bootstrap-datetimepicker'], function () {
                        var options = {
                            format: 'YYYY-MM-DD HH:mm:ss',   //
                            icons: {
                                time: 'fa fa-clock-o',
                                date: 'fa fa-calendar',
                                up: 'fa fa-chevron-up',
                                down: 'fa fa-chevron-down',
                                previous: 'fa fa-chevron-left',
                                next: 'fa fa-chevron-right',
                                today: 'fa fa-history',
                                clear: 'fa fa-trash',
                                close: 'fa fa-remove'
                            },
                            showTodayButton: true,
                            showClose: true
                        };
                        $('.datetimepicker', form).parent().css('position', 'relative');
                        $('.datetimepicker', form).datetimepicker(options);
                    });
                }
            });
            var submitExportForm = function (ids, layero, index) {
                var data = {
                    ids: ids,
                    task_id: $('#task_id').val(),
                    start_time: $("#c-start_time", layero).val(),
                    end_time: $("#c-end_time", layero).val()
                };
                Fast.api.ajax({
                    url: Fast.api.fixurl('learn/orgpay/addorg'),
                    data: data
                }, function (res) {
                    Layer.closeAll();
                    if(res.error == 1){
                        Layer.alert(res.message);
                    }else{
                        Fast.api.close(layero.selector);
                        top.window.$("ul.nav-addtabs .active").trigger("click");
                    }
                    // top.window.$("ul.nav-addtabs li.active").find(".fa-remove").trigger("click");
                });
            };
            // Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});